package com.huizu.project.controller;

import java.util.List;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import com.huizu.common.core.domain.entity.SysUser;
import com.huizu.common.utils.SecurityUtils;
import com.huizu.project.domain.SysUserAuth;
import com.huizu.project.service.ISysUserAuthService;
import com.huizu.project.utils.TokenUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.huizu.common.annotation.Log;
import com.huizu.common.core.controller.BaseController;
import com.huizu.common.core.domain.AjaxResult;
import com.huizu.common.enums.BusinessType;
import com.huizu.project.domain.ProNj;
import com.huizu.project.service.IProNjService;
import com.huizu.common.utils.poi.ExcelUtil;
import com.huizu.common.core.page.TableDataInfo;

import static com.huizu.common.utils.PageUtils.startPage;

/**
 * 年级管理Controller
 * 
 * @author zhangmx
 * @date 2024-05-11
 */
@RestController
@RequestMapping("/project/pronj")
public class ProNjController extends BaseController
{
    @Autowired
    private IProNjService proNjService;

    @Autowired
    private ISysUserAuthService iSysUserAuthService;



    /**
     * 查询年级管理列表
     */
    @PreAuthorize("@ss.hasPermi('project:pronj:list')")
    @GetMapping("/list")
    public TableDataInfo list(ProNj proNj) {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        if (!SecurityUtils.isAdmin(user.getUserId())) {
            SysUserAuth sysUserAuthPram = new SysUserAuth();
            sysUserAuthPram.setUserId(user.getUserId());
            List<SysUserAuth> auths = iSysUserAuthService.selectSysUserAuthList(sysUserAuthPram);
            List<String> njIds = auths.stream().filter(auth -> auth.getType().equals("nj")).map(SysUserAuth::getBusId).collect(Collectors.toList());
            proNj.setNjIds(njIds);
        }
        startPage();
        List<ProNj> list = proNjService.selectProNjList(proNj);
        return getDataTable(list);
    }

    /**
     * 查询年级管理列表
     */
    //@PreAuthorize("@ss.hasPermi('project:pronj:list')")
    @GetMapping("/listTotal")
    public TableDataInfo listTotal(ProNj proNj) {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        if (!SecurityUtils.isAdmin(user.getUserId())) {
            SysUserAuth sysUserAuthPram = new SysUserAuth();
            sysUserAuthPram.setUserId(user.getUserId());
            List<SysUserAuth> auths = iSysUserAuthService.selectSysUserAuthList(sysUserAuthPram);
            List<String> njIds = auths.stream().filter(auth -> auth.getType().equals("nj")).map(SysUserAuth::getBusId).collect(Collectors.toList());
            proNj.setNjIds(njIds);
        }
        List<ProNj> list = proNjService.selectProNjList(proNj);
        return getDataTable(list);
    }

    /**
     * 导出年级管理列表
     */
    @PreAuthorize("@ss.hasPermi('project:pronj:export')")
    @Log(title = "年级管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ProNj proNj) {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        if (!SecurityUtils.isAdmin(user.getUserId())) {
            SysUserAuth sysUserAuthPram = new SysUserAuth();
            sysUserAuthPram.setUserId(user.getUserId());
            List<SysUserAuth> auths = iSysUserAuthService.selectSysUserAuthList(sysUserAuthPram);
            List<String> njIds = auths.stream().filter(auth -> auth.getType().equals("nj")).map(SysUserAuth::getBusId).collect(Collectors.toList());
            proNj.setNjIds(njIds);
        }
        List<ProNj> list = proNjService.selectProNjList(proNj);
        ExcelUtil<ProNj> util = new ExcelUtil<ProNj>(ProNj.class);
        util.exportExcel(response, list, "年级管理数据");
    }

    /**
     * 获取年级管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('project:pronj:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return success(proNjService.selectProNjById(id));
    }

    /**
     * 新增年级管理
     */
    @PreAuthorize("@ss.hasPermi('project:pronj:add')")
    @Log(title = "年级管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ProNj proNj)
    {
        return toAjax(proNjService.insertProNj(proNj));
    }

    /**
     * 修改年级管理
     */
    @PreAuthorize("@ss.hasPermi('project:pronj:edit')")
    @Log(title = "年级管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ProNj proNj)
    {
        return toAjax(proNjService.updateProNj(proNj));
    }

    /**
     * 删除年级管理
     */
    @PreAuthorize("@ss.hasPermi('project:pronj:remove')")
    @Log(title = "年级管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(proNjService.deleteProNjByIds(ids));
    }
}
